<template>
  <div class="Home">
    <div class="banner" :style="{ backgroundImage: `url(${mpImgs['home_banner_bg']})` }"></div>
    <div class="login-panel" v-if="!isLogin">
      <button
        hover-class="hover"
        hover-start-time="0"
        hover-stay-time="0"
        class="login-btn"
        @click="handleLogin">点击登录</button>
    </div>
    <div class="login-panel" v-else>
      <div class="left">
        <img :src="generateImgUrl(userInfo.photoUrl)" mode="aspectFill" />
      </div>
      <div class="right">
        <div class="name">{{ userInfo.realName }}</div>
        <div class="tags">
          <div class="tag" v-if="userInfo.certificate">已实名</div>
          <div class="tag dark" v-else @click="handleFace">未实名</div>
          <div class="tag" v-if="userInfo.npcmember">人大代表</div>
        </div>
      </div>
      <div v-if="userInfo.certificate && userInfo.npcmember" class="right-icon" @click="open">
        <van-icon name="arrow" color="#DDDDDD" size="20px"/>
      </div>
    </div>
    <div class="nav-wrap">
      <div class="parent-list" v-for="item in homeNav" :key="item.parentName">
        <div class="title">{{ item.parentName }}</div>
        <div class="list">
          <div
            class="item"
            hover-class="hover"
            hover-start-time="0"
            hover-stay-time="0"
            v-for="item2 in item.list"
            :key="item2.id"
            @click="handleGo(item2.value)"
          >
            <img :src="generateImgUrl(item2.icon)" mode="aspectFill" />
            <div class="text">{{ item2.name }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import wxp from '@/utils/wxp'
import api from '@/api'
import cache from '@/utils/cache'
import { navToFace } from '@/utils/face'
import { generateImgUrl, getParams } from '@/utils'

export default {
  data () {
    return {
      generateImgUrl,
    }
  },
  computed: {
    mpImgs () {
      return this.$store.state.mpImgs
    },
    userInfo () {
      return this.$store.state.userInfo
    },
    homeNav () {
      return this.$store.state.homeNav
    },
    isLogin () {
      return this.$store.state.isLogin
    },
  },
  methods: {
    async handleLogin () {
      wx.getUserProfile({
        desc: '用于完善资料',
        success: async res => {
          const { code } = await wxp.login()
          const { result } = await api.wxMpLogin({
            code,
            name: encodeURIComponent(res.userInfo.nickName),
            photoUrl: res.userInfo.avatarUrl,
          })
          cache.Authorization.set(result.token)
          this.$store.dispatch('init')
        },
      })
    },
    handleFace () {
      navToFace()
    },
    handleGo (url) {
      // 未登录和实名认证和人大代表限制
      if (!this.isLogin) {
        uni.showModal({
          title: '未登录',
          content: '请先进行登录',
          showCancel: false,
        })
      } else if (!this.userInfo.certificate) {
        uni.showModal({
          title: '未实名认证',
          content: '很抱歉，您还未实名认证，是否立刻实名认证？',
          success ({ confirm, cancel }) {
            if (confirm) {
              navToFace()
            }
            if (cancel) {
              uni.navigateBack()
            }
          },
        })
      } else if (!this.userInfo.npcmember) {
        uni.showModal({
          title: '非人大代表',
          content: '很抱歉，您不是人大代表，无法使用此功能！请联系所在选区管理员，在后台添加人大代表档案信息！',
          showCancel: false,
        })
      } else if (url.startsWith('action')) {
        wx[url.split(':')[1]]()
      } else {
        this.go(url)
      }
    },
    go (url) {
      uni.navigateTo({
        url,
      })
      uni.switchTab({
        url,
      })
      this.$store.commit('SET_TAB_BAR_PARAMS', {
        time: Date.now(),
        params: getParams(url),
      })
    },
    open () {
      uni.navigateTo({
        url: '/pages/userinfo/index',
      })
    },
  },
}
</script>

<style lang="scss" scoped>
@import "~css/var.scss";

.Home {
	background: #fff;

	.banner {
		height: 178px;
		background-size: 100% 100%;
	}

	.login-panel {
		display: flex;
		align-items: center;
		width: 324px;
		height: 138px;
		margin: 0 auto;
		margin-top: -80px;
		border-radius: 5px;
		box-shadow: 0 0 15px 0 rgba(255, 80, 66, .35);
		background: #fff;

		.login-btn {
			width: 126px;
			border: none;
			border-radius: 20px;
			line-height: 40px;
			font-size: $fz-xl;
			color: #fff;
			background: #e51e1e;

			&.hover {
				opacity: .8;
			}
		}

		.left {
			width: 100px;
			height: 100px;
			margin-left: 20px;

			img {
				width: 100%;
				height: 100%;
				border-radius: 50%;
			}
		}

		.right {
			margin-left: 20px;

			.name {
				line-height: 24px;
				font-weight: 800;
				font-size: $fz-l;
				color: #333;
			}

			.tags {
				display: flex;
				margin-top: 18px;

				.tag {
					margin-right: 8px;
					padding: 0;
					padding: 0 10px;
					border: 1px solid #e51e1e;
					border-radius: 3px;
					line-height: 21px;
					font-size: $fz-s;
					color: #fff;
					background: #e51e1e;

					&.dark {
						border: 1px solid #e51e1e;
						color: #e51e1e;
						background: #fff;
					}
				}
			}
		}

    .right-icon {
      height: 100px;
      line-height: 100px;
      margin-left: 10px;
    }
	}

	.nav-wrap {
		padding-bottom: 43px;

		.parent-list {
			margin-top: 29px;

			.title {
				margin-left: 16px;
				font-weight: bold;
				font-size: $fz-xl;
				color: #333;
			}

			.list {
				display: flex;
				flex-wrap: wrap;

				.item {
					display: flex;
					flex-direction: column;
					align-items: center;
					flex: 0 0 20%;
					box-sizing: border-box;
					margin: 10px 0;
					padding: 10px 0;
					border-radius: 8px;

					img {
						width: 36px;
						height: 36px;
					}

					.text {
						margin-top: 10px;
						font-weight: 500;
						font-size: $fz-m;
						color: #666;
					}

					&.hover {
						background: rgb(243, 243, 243);
					}
				}
			}
		}
	}
}
</style>
